//
//  SHRetweetSectionView.swift
//  SHWeibo
//
//  Created by jingming wang on 16/7/28.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit
import SnapKit

class SHRetweetSectionView: UIView {
    
    // 记录转发微博的底部约束
    var retweetViewBottomConstraint: Constraint?
    
    // MARK: - 懒加载控件
    // 文字内容
    private lazy var contentLabel: UILabel = UILabel(text: "", textColor: UIColor.darkGrayColor(), font: 15,numberOfLines: 0, maxWidth: K_SCREEN_WIDTH - homeCellMargin * 2)
    // 配图
    private lazy var pictureView: SHPictureView = SHPictureView()
    
    // 数据传递
    var userInfoViewModel: SHUserInfoViewModel? {
        didSet {
            
            contentLabel.attributedText = userInfoViewModel?.retweetContentAttr
            // 卸载约束
            retweetViewBottomConstraint?.uninstall()
            // 代表有配图
            if userInfoViewModel?.data?.retweeted_status?.pic_urls?.count > 0 {
                // 01 赋值
                pictureView.pictuerModels = userInfoViewModel?.data?.retweeted_status?.pic_urls
                // 02 更改约束
                self.snp_makeConstraints { (make) -> Void in
                    self.retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(homeCellMargin).constraint
                }
                // 03 显示配图
                pictureView.hidden = false
            }else {
                // 代表没有配图
                
                // 01 更改约束
                self.snp_makeConstraints { (make) -> Void in
                    self.retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(homeCellMargin).constraint
                }
                // 02 显示配图
                pictureView.hidden = true
            }

        }
    }
   
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置界面
    private func setupUI() {
        backgroundColor = RGB(237, green: 237, blue: 237)
        
        addSubview(contentLabel)
        addSubview(pictureView)
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(homeCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            // 当前size约束已经在配图中添加了
            // make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(homeCellMargin)
            make.leading.equalTo((K_SCREEN_WIDTH - (cellWH*3 + cellMargin*2))/2)
        }
        // 约束当前view
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(homeCellMargin).constraint
        }
    }

}
